﻿using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.Dz_ConvertBill.XHX
{
    [Description("单据转换插件:销售出库下推报关单汇总"), HotUpdate]
    public class PackinglistConvertPackinglist : AbstractConvertPlugIn
    {
        private object fid;
        public override void OnGetSourceData(GetSourceDataEventArgs e)
        {
            base.OnGetSourceData(e);
            var sobj = e.SourceData.FirstOrDefault();
            fid = sobj["FID"];
        }
        public override void OnAfterFieldMapping(AfterFieldMappingEventArgs e)
        {
            base.OnAfterFieldMapping(e);
            var billHead = e.TargetExtendDataEntitySet.FindByEntityKey("FBillHead");
            //报关单汇总
            object[] obs = new object[1];
            obs[0] = fid;
            var targetBillMeta = MetaDataServiceHelper.Load(this.Context, "SAL_OUTSTOCK") as FormMetadata;
            var obj = BusinessDataServiceHelper.Load(this.Context, obs, targetBillMeta.BusinessInfo.GetDynamicObjectType()).FirstOrDefault();
            DynamicObjectCollection entityCollection = obj["SAL_OUTSTOCKENTRY"] as DynamicObjectCollection;
            for (int k = 0; k < billHead.Count(); k++)
            {
                SetData(billHead[k], entityCollection, e.TargetBusinessInfo);
            }
        }

        private void SetData(ExtendedDataEntity billHead, DynamicObjectCollection entityCollection, BusinessInfo targetBusinessInfo)
        {
            DynamicObjectCollection srcCollection = billHead.DataEntity["FEntity"] as DynamicObjectCollection;
            //汇总信息单据体
            srcCollection.Clear();

            int i = 0;
            //英文品名，中文品名，对照表编码（型号）汇总 F_YEA_Qty
            foreach (var typeb in (from p in entityCollection
                                   group p by new
                                   {
                                       GoodsNo = Convert.ToString(p["F_GoodsNo"]),
                                       Ename = Convert.ToString(p["F_EnName"]),
                                       CustomerType = Convert.ToString(p["CustMatID"]),
                                       BaseUnitID = Convert.ToString(p["BaseUnitID"])
                                   } into g
                                   select new
                                   {
                                       GoodsNo = g.Key.GoodsNo,
                                       Ename = g.Key.Ename,
                                       CustomerType = g.Key.CustomerType,
                                       BaseUnitID = g.Key.BaseUnitID,
                                       Price = g.Sum(p => Convert.ToDecimal(p["Amount"])) / g.Sum(p => Convert.ToDecimal(p["PriceUnitQty"])),
                                       Qty = g.Sum(p => Convert.ToDecimal(p["PriceUnitQty"])),
                                       Account = g.Sum(p => Convert.ToDecimal(p["Amount"])),
                                       F_CTNS = g.Sum(p => Convert.ToInt32(p["F_CTNS"]))
                                   }))
            {
                i++;
                DynamicObject dyo = new DynamicObject(srcCollection.DynamicCollectionItemPropertyType);
                dyo["seq"] = i;
                dyo["F_GOODSNO"] = typeb.GoodsNo;
                //英文品名
                dyo["F_GOODSNAME"] = typeb.Ename;
                //客户规格型号
                dyo["F_Spec"] = typeb.CustomerType;
                dyo["F_Qty"] = typeb.Qty;
                dyo["F_UnitID"] = typeb.BaseUnitID;
                dyo["F_Price"] = typeb.Price;
                dyo["F_Amount"] = typeb.Account;
                dyo["F_CTNS"] = typeb.F_CTNS;

                srcCollection.Add(dyo);
            }
        }
    }
}
